package com.ctgu.computer.controller.admin;

import com.ctgu.computer.common.FailedMessage;
import com.ctgu.computer.common.SuccessMessage;
import com.ctgu.computer.common.UserLocalTread;
import com.ctgu.computer.domain.dto.AdminAddDTO;
import com.ctgu.computer.domain.dto.AdminLoginDTO;
import com.ctgu.computer.domain.dto.PageQueryDTO;
import com.ctgu.computer.domain.dto.UserPageDTO;
import com.ctgu.computer.domain.po.AdminInfo;
import com.ctgu.computer.domain.result.PageResult;
import com.ctgu.computer.domain.result.Result;
import com.ctgu.computer.domain.vo.AdminLoginVO;
import com.ctgu.computer.prop.JwtProperties;
import com.ctgu.computer.service.AdminService;
import com.ctgu.computer.service.LoginService;
import com.ctgu.computer.service.UserService;
import com.ctgu.computer.utils.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/admin")
@Tag(name = "管理员相关接口")
public class AdminController {

    // 这里采用构造方式注入bean,下面如果需要注入Service层不用加@Autowired 或 @Resource
    private final LoginService loginService;
    private final JwtProperties jwtProperties;
    private final AdminService adminService;
    private final UserService userService;

    @Operation(summary = "管理员登录")
    @PostMapping("/login")
    public Result<AdminLoginVO> login(@RequestBody AdminLoginDTO adminLoginDTO){
        AdminInfo admin = loginService.login(adminLoginDTO);
        if(admin==null)
            return Result.error(FailedMessage.LOGIN_FAIL);
        String jwt = JwtUtils.createJWT(Map.of("adminId", admin.getAdminId()),
                jwtProperties.getSignKey(),
                jwtProperties.getExpireTime());

        return Result.success(SuccessMessage.LOGIN_SUCCESS,new AdminLoginVO(jwt));
    }

    @Operation(summary = "获取当前管理员信息")
    @GetMapping("/current")
    public Result<AdminInfo> getCurrentAdminInfo(){
        return Result.success(adminService.getInfo());
    }
    @Operation(summary = "新增管理员")
    @PostMapping("/add")
    public Result addAdmin(@RequestBody AdminAddDTO adminAddDTO){
        AdminInfo admin = loginService.addAdmin(adminAddDTO);
        //生成jwt令牌 claims命名为adminId
        return Result.success(SuccessMessage.ADD_SUCCESS);
    }

    @Operation(summary = "批量删除管理员")
    @PostMapping("/delete")
    public Result deleteUser(@RequestBody List<Long> ids){
        System.out.println("管理员的id"+ids);
        adminService.deleteByIds(ids);
        return Result.success(SuccessMessage.DELETE_SUCCESS);
    }

    @Operation(summary = "分页查询管理员")
    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody PageQueryDTO pageQueryDTO){
            return Result.success(adminService.page(pageQueryDTO));
    }

    @Operation(summary = "根据id查询管理员")
    @PostMapping("/select/{adminId}")
    public Result<AdminInfo> getById(@PathVariable Integer adminId){
        return Result.success(adminService.getById(adminId));
    }

    @Operation(summary = "修改管理员信息")
    @PostMapping("/update")
    public Result update(@RequestBody AdminInfo adminInfo){
        adminInfo.setPassword(null);
        adminService.updateAdminInfo(adminInfo);
        return Result.success(SuccessMessage.UPDATE_SUCCESS);
    }

    @Operation(summary = "批量修改用户状态")
    @PostMapping("/update/user")
    public Result updateUserStatus(@RequestBody List<Integer> ids){
        log.info("批量修改用户状态参数:{}",ids);
        userService.updateStatus(ids);
        return Result.success(SuccessMessage.UPDATE_SUCCESS,null);
    }

    @Operation(summary = "分页查询所有用户信息")
    @PostMapping("/page/user")
    public Result<PageResult> getAllUser(@RequestBody UserPageDTO userPageDTO){
        return Result.success(userService.page(userPageDTO));
    }

    @Operation(summary = "批量删除用户信息")
    @PostMapping("/delete/user")
    public Result deleteUserInfo(@RequestBody List<Integer> ids){
        if(UserLocalTread.getAdminId()==null){
            return Result.error(FailedMessage.NO_PERMISSION);
        }
        userService.batchDelete(ids);
        return Result.success(SuccessMessage.DELETE_SUCCESS,null);
    }

}
